package com.yonyou.crm.sys.modules.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.yonyou.crm.bpub.list.entity.AppListVO;
import com.yonyou.crm.bpub.list.entity.AppListValueVO;
import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.common.exception.CrmBusinessException;
import com.yonyou.crm.common.login.context.LoginContextConstant;
import com.yonyou.crm.sys.modules.constants.ClientType;
import com.yonyou.crm.sys.modules.entity.FieldVO;
import com.yonyou.crm.sys.modules.entity.LayoutVO;
import com.yonyou.crm.sys.modules.entity.ListLayoutAssignmentVO;
import com.yonyou.crm.sys.modules.entity.ListLayoutVO;
import com.yonyou.crm.sys.modules.entity.ListLayoutItemVO;
import com.yonyou.crm.sys.modules.entity.ModuleVO;
import com.yonyou.crm.sys.modules.events.FieldDeleteEvent;
import com.yonyou.crm.sys.role.entity.RoleVO;
import com.yonyou.crm.sys.role.service.RoleBizServiceImpl;
import com.yonyou.iuap.context.InvocationInfoProxy;

/**
 * 
 * @author xugm@yonyou.com
 *
 */
@Service
public class ListLayoutBizServiceImpl extends BaseBizServiceImpl<ListLayoutVO> {
	@Autowired
	private CustomIdGeneratorBizServiceImpl customIdGeneratorBizServiceImpl;
	@Autowired
	private ModuleBizServiceImpl moduleBizServiceImpl;
	@Autowired
	private FieldBizServiceImpl fieldBizServiceImpl;
	
	private ListLayoutItemRepository listLayoutItemRepository;
	
	@Autowired
	private RoleBizServiceImpl roleBizServiceImpl;
	@Autowired
	private AdministratorManage administratorManage;
	
	public ListLayoutBizServiceImpl() {
		super();
		listLayoutItemRepository = new ListLayoutItemRepository();
	}

	public ListLayoutVO saveListLayout(ListLayoutVO listLayoutVO) {
		String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		String userIdStr = (String) InvocationInfoProxy.getParameter("id");// 用户id
		Long userId = Long.parseLong(userIdStr);
		Long listLayoutId = getNextId();
		listLayoutVO.setTenantId(tenantId);
		listLayoutVO.setId(listLayoutId);
		// 列表布局新增
		Date now = new Date();
		Date createDate = (listLayoutVO.getCreatedTime() == null) ? now
				: listLayoutVO.getCreatedTime();
		Date modifyDate = (listLayoutVO.getModifiedTime() == null) ? now
				: listLayoutVO.getModifiedTime();
		Long createUserId = (listLayoutVO.getCreatedUserId() == null) ? userId
				: listLayoutVO.getCreatedUserId();
		Long modifyUserId = (listLayoutVO.getModifiedUserId() == null) ? userId
				: listLayoutVO.getModifiedUserId();
		listLayoutVO.setCreatedTime(createDate);
		listLayoutVO.setModifiedTime(modifyDate);
		listLayoutVO.setCreatedUserId(createUserId);
		listLayoutVO.setModifiedUserId(modifyUserId);
		listLayoutVO.setSysCreatedTime(now);
		listLayoutVO.setSysModifiedTime(now);
		listLayoutVO.setSysCreatedUserId(userId);
		listLayoutVO.setSysModifiedUserId(userId);
		listLayoutVO.setSortOrder(now.getTime());
		listLayoutVO.setIsDefault(0);
		listLayoutVO.setEnableState(1);//1代表默认启用状态
		listLayoutVO.setEnableTime(now);
		listLayoutVO.setEnableUserId(userId);
		listLayoutVO = super.saveVO(listLayoutVO);
		// 列表布局明细新增
		if (listLayoutVO.getItems() != null && listLayoutVO.getItems().size()>0) {
			ModuleVO moduleVO = moduleBizServiceImpl.getModule(
					listLayoutVO.getObjId());
			for (int i = 0; i < listLayoutVO.getItems().size(); i++) {
				ListLayoutItemVO listLayoutItemVO = listLayoutVO.getItems().get(i);
				listLayoutItemVO.setListLayoutId(listLayoutId);
			}
			listLayoutItemRepository.deleteItems(listLayoutId);
			listLayoutItemRepository.addItems(moduleVO,listLayoutVO.getItems());
		}
		return listLayoutVO;
	}

	public ListLayoutVO updateListLayout(ListLayoutVO listLayoutVO) {
		String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		String userIdStr = (String) InvocationInfoProxy.getParameter("id");// 用户id
		Long userId = Long.parseLong(userIdStr);
		Long listLayoutId = listLayoutVO.getId();
		Date now = new Date();
		listLayoutVO.setTenantId(tenantId);
		listLayoutVO.setModifiedTime(now);
		listLayoutVO.setModifiedUserId(userId);
		listLayoutVO.setSysModifiedUserId(userId);
		listLayoutVO.setSysModifiedTime(now);

		ListLayoutVO originalLayoutVO = getListLayout(listLayoutId);
		listLayoutVO.setObjId(originalLayoutVO.getObjId());
		listLayoutVO.setClientType(originalLayoutVO.getClientType());
		listLayoutVO.setIsDefault(originalLayoutVO.getIsDefault());
		ModuleVO moduleVO = moduleBizServiceImpl.getModule(originalLayoutVO.getObjId());
		if (originalLayoutVO.getTenantId() == 0) {
			// 如果是系统预置的，则从系统预置的数据那里复制一份出来
			listLayoutVO.setObjId(originalLayoutVO.getObjId());
			listLayoutVO.setIsDefault(1);
			listLayoutVO.setSysCreatedUserId(userId);
			listLayoutVO.setSysCreatedTime(now);
			listLayoutVO.setCreatedTime(originalLayoutVO.getCreatedTime());
			listLayoutVO.setCreatedUserId(originalLayoutVO.getCreatedUserId());
			listLayoutVO.setSortOrder(originalLayoutVO.getSortOrder());
			super.saveVO(listLayoutVO);
		} else {
			super.updateVO(listLayoutVO);
		};
		// 处理明细，先删后增
		listLayoutItemRepository.deleteItems(listLayoutId);
		listLayoutItemRepository.addItems(moduleVO,listLayoutVO.getItems());
		// 返回给前端使用
		listLayoutVO.setRoles(originalLayoutVO.getRoles());
		return listLayoutVO;
	}

	public ListLayoutVO getListLayout(Long id) {
		String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("tenantId", tenantId);
		paramMap.put("id", id);
		List<ListLayoutVO> listLayoutList =  dao.selectVoList(ListLayoutVO.class, "ListListLayout", paramMap);
		ListLayoutVO aListLayoutVO = new ListLayoutVO();
		for (ListLayoutVO listLayoutVO : listLayoutList) {
			aListLayoutVO = listLayoutVO;
			if (listLayoutVO.getTenantId() != 0) {
					break;
			}
		}
		List<ListLayoutItemVO> items = listLayoutItemRepository.getItems(aListLayoutVO);
		List<RoleVO> roleList = new ArrayList<RoleVO>();
		if (aListLayoutVO.getIsDefault() != 0) {
			Map<String, Object> param = new HashMap<String, Object>();
			paramMap.put("tenantId", tenantId);
			paramMap.put("listLayoutId", id);
			List<ListLayoutAssignmentVO> assignmentVOs= dao.selectVoList(ListLayoutAssignmentVO.class, "ListListLayoutAssignment", param);
			Iterator<ListLayoutAssignmentVO> it = assignmentVOs.iterator();
			while (it.hasNext()) {
					ListLayoutAssignmentVO aLayoutAssignmentVO = it.next();
					// TODO 批量获取获取角色列表
					if (aListLayoutVO.getTenantId().equals(aLayoutAssignmentVO.getTenantId())) {
			    		RoleVO role  = roleBizServiceImpl.getROleDetail(aLayoutAssignmentVO.getRoleId());
			    		roleList.add(role);
					}
			} 
		}
		aListLayoutVO.setRoles(roleList);
		aListLayoutVO.setItems(items);
		transformForClient(aListLayoutVO);
		return aListLayoutVO;
	}
	public List<ListLayoutVO>  getListLayoutList(Long objId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		paramMap.put("tenantId", tenantId);
		paramMap.put("objId", objId);
		List<ListLayoutVO> layoutList =  dao.selectVoList(ListLayoutVO.class, "ListListLayout", paramMap);
		List<Long> customLayouts = new ArrayList<Long>();
		for (ListLayoutVO layoutVO : layoutList) {
			if (layoutVO.getTenantId() != 0) {
				customLayouts.add(layoutVO.getId());
			}
		}
		Iterator<ListLayoutVO> it = layoutList.iterator();
		while (it.hasNext()) {
			ListLayoutVO layout = it.next();
			if ((layout.getTenantId() == 0 && customLayouts.contains(layout.getId())) 
					|| layout.getIsDeleted() ==1) {
				    it.remove();
			}
		}
		return layoutList;
	}

	private Long getNextId() {
		return customIdGeneratorBizServiceImpl.getId();
	}

	private class ListLayoutItemRepository {

		/**
		 * 删除明细布局所有字段
		 * 
		 * @return
		 */
		public int deleteItems(Long listLayoutId) {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
			Long tenantId = Long.parseLong(tenantIdStr);
			paramMap.put("tenantId", tenantId);
			paramMap.put("listLayoutId", listLayoutId);
			return dao.delete(ListLayoutItemVO.class, "deleteAllItems",	paramMap);
		}

		public int addItems(ModuleVO moduleVO,List<ListLayoutItemVO> listLayoutItemVOs) {
			String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
			Long tenantId = Long.parseLong(tenantIdStr);
			String userIdStr = (String) InvocationInfoProxy.getParameter("id");// 用户id
			Long userId = Long.parseLong(userIdStr);
			Date now = new Date();
			for (int i = 0; i < listLayoutItemVOs.size(); i++) {
				ListLayoutItemVO listLayoutItemVO = listLayoutItemVOs.get(i);
				if (listLayoutItemVO.getIsBlank() ==0) {
					FieldVO fieldVO = fieldBizServiceImpl.getField(moduleVO.getId(), listLayoutItemVO.getApiName());
					listLayoutItemVO.setActualFieldFullname(fieldVO.getFullname());
				} else {
					listLayoutItemVO.setActualFieldFullname("");
				}
				listLayoutItemVO.setTenantId(tenantId);
				listLayoutItemVO.setSysCreatedTime(now);
				listLayoutItemVO.setSysModifiedTime(now);
				listLayoutItemVO.setSysCreatedUserId(userId);
				listLayoutItemVO.setSysModifiedUserId(userId);
			}
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("list", listLayoutItemVOs);
			return dao.saveSql(ListLayoutItemVO.class, "batchInsertItems",	paramMap);
		}
		
		public List<ListLayoutItemVO> getItems(ListLayoutVO listLayoutVO) {
			String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
			Long tenantId = Long.parseLong(tenantIdStr);
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("tenantId", tenantId);
			paramMap.put("listLayoutId", listLayoutVO.getId());			
           List<ListLayoutItemVO> listLayoutItemVOs = dao.selectVoList(ListLayoutItemVO.class, "ListListLayoutItem", paramMap);
           Boolean isCustom = false;
			for (ListLayoutItemVO listLayoutItemVO : listLayoutItemVOs) {
				if (listLayoutItemVO.getTenantId() != 0L) {
					isCustom = true;
				}
			}
			if (isCustom) {
		           Iterator<ListLayoutItemVO> it = listLayoutItemVOs.iterator();
		           while(it.hasNext()) {
		        	   ListLayoutItemVO listLayoutItemVO = it.next();
		        	   if (listLayoutItemVO.getTenantId() == 0L) {
		        		   it.remove();
		        	   }
		           }
			}
			for (ListLayoutItemVO listLayoutItemVO : listLayoutItemVOs) {
				if (listLayoutItemVO.getIsBlank() ==0) {
					FieldVO field = fieldBizServiceImpl.getField(listLayoutItemVO.getActualFieldFullname());
					listLayoutItemVO.setActualField(field);
				}
			}		
           return listLayoutItemVOs;
		}
	}
	
	public void saveListLayoutAssignment(Integer clientType, List<ListLayoutAssignmentVO> assignmentVOs) {

		String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		Map<String, Object> assignMap = new HashMap<String, Object>();
		assignMap.put("tenantId", tenantId);
		assignMap.put("clientType", clientType);
		assignMap.put("assignments", assignmentVOs);
		// 删除原来的配置
		dao.deleteSql(ListLayoutAssignmentVO.class, "deleteAllItems",	assignMap);
		// 重新分配
		dao.saveSql(ListLayoutAssignmentVO.class, "insertListLayoutAssignment", assignMap);
	}

	/**
	 * 获取当前用户列表模板。
	 * @param objId
	 * @param clientType
	 * @return
	 */
	public  ListLayoutVO getListLayoutForCurrentUser(Long objId,Integer clientType) {

		Map<String, Object> paramMap = new HashMap<String, Object>();
		String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		String roleIdStr = InvocationInfoProxy.getParameter(LoginContextConstant.RoleId);
		Long roleId = Long.parseLong(roleIdStr);
		
		paramMap.put("tenantId", tenantId);
		paramMap.put("clientType", clientType);
		paramMap.put("roleId", roleId);
		List<ListLayoutAssignmentVO> assignmentVOs= dao.selectVoList(ListLayoutAssignmentVO.class, "ListListLayoutAssignment", paramMap);
		
		ListLayoutVO layoutVO = null;
		ListLayoutAssignmentVO layoutAssignmentVO = null;
		for (ListLayoutAssignmentVO assignmentVO : assignmentVOs) {
			layoutAssignmentVO = assignmentVO;
			if (assignmentVO.getTenantId() != 0) {
				break;
			}
		}
		if (layoutAssignmentVO == null) {
			layoutVO = getDefaultListLayout(objId, clientType);
		} else {
			layoutVO = getListLayout(layoutAssignmentVO.getListLayoutId());
		}
		return layoutVO;
	}
	
	public ListLayoutVO getDefaultListLayout(Long objId,Integer clientType) {
		List<ListLayoutVO> layoutVOs = getListLayoutList(objId);
		ListLayoutVO defaultLayoutVO = null;
		for (ListLayoutVO layoutVO:layoutVOs) {
			if (layoutVO.getIsDefault() == 1 && layoutVO.getClientType()==clientType) {
				defaultLayoutVO = getListLayout(layoutVO.getId());
				break;
			}
		}
		if (defaultLayoutVO == null) {
			throw new CrmBusinessException("未设置默认列表模板");
		}
		return defaultLayoutVO;
	}
	
	@EventListener
	public void onFieldDelete(FieldDeleteEvent event) {
		// 对级联字段的判断比较麻烦，需要判断是否有作为中间字段用到，即本字段没在模板中使用，但是作为中间字段为别的字段使用了，
		// 当前没有级联字段，暂时不管，只用actualField来做判断
		FieldVO fieldVO = event.getField();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("actualField", fieldVO.getFullname());
		List<ListLayoutVO> layoutList = dao.selectVoList(ListLayoutVO.class, "ListListLayoutUsingField", paramMap);
		if (layoutList.size() > 0) {
			List<String> layoutNames = new ArrayList<String>();
			for (ListLayoutVO layoutVO : layoutList) {
				layoutNames.add(layoutVO.getName());
			}
			String layouts = StringUtils.collectionToCommaDelimitedString(layoutNames);		
			String msg = "列表模板"+layouts+"用到了该字段，请先将字段从模板中移除";
			throw new CrmBusinessException(msg);
		}
	}
	
	public void deleteListLayout(Long id) {
		String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		String userIdStr = (String) InvocationInfoProxy.getParameter("id");// 用户id
		Long userId = Long.parseLong(userIdStr);
		Date now = new Date();
		ListLayoutVO originalLayoutVO = getListLayout(id);
		originalLayoutVO.setSysModifiedTime(now);
		originalLayoutVO.setSysModifiedUserId(userId);
		originalLayoutVO.setIsDeleted(1);
		if (originalLayoutVO.getTenantId() == 0) {
			// 如果是系统预置的，则从系统预置的数据那里复制一份出来
			originalLayoutVO.setTenantId(tenantId);
			originalLayoutVO.setSysCreatedUserId(userId);
			originalLayoutVO.setSysCreatedTime(now);
			super.saveVO(originalLayoutVO);
		} else {
			super.updateVO(originalLayoutVO);
		};
		listLayoutItemRepository.deleteItems(id);
	}
	
	private void transformForClient(ListLayoutVO listLayoutVO) {
			List<ListLayoutItemVO> listLayoutItemVOs = listLayoutVO.getItems();
			List<Integer> rowFlag = new ArrayList<Integer>();
			List<List<ListLayoutItemVO> > itemsForClient = new ArrayList<List<ListLayoutItemVO>>();
			for (Integer  j=0;j<listLayoutItemVOs.size();j++) {
				ListLayoutItemVO item = listLayoutItemVOs.get(j);
					Integer row = item.getRowPosition();
					if (!rowFlag.contains(row)) {
						rowFlag.add(row);
						List<ListLayoutItemVO> itemVOs = new ArrayList<ListLayoutItemVO>();
						itemVOs.add(item);
						itemsForClient.add(itemVOs);
					}	else {
						itemsForClient.get(itemsForClient.size()-1).add(item);
					}	
			}
			listLayoutVO.setItemsForClient(itemsForClient);
		}
	
	/**
	 * 列表模板分配模板
	 */
	public Map<String, Object> getAssignmentTemplate(Long objId, Integer clientType ) {
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		// 角色列表
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("tenantId", new Long(tenantId));
		List<RoleVO> roleVOs = roleBizServiceImpl.getRoleListByTenantId(param);
		List<Map<String, Object>> roleMaps = new ArrayList<Map<String,Object>>();
		for (RoleVO roleVO: roleVOs) {
			Map<String, Object> roleMap = new HashMap<String, Object>();
			roleMap.put("id", roleVO.getId());
			roleMap.put("name", roleVO.getName());
			roleMaps.add(roleMap);
		}
		// 布局分配
		paramMap.put("tenantId", tenantId);
		paramMap.put("clientType", clientType);
		List<ListLayoutAssignmentVO> assignmentVOs = dao.selectVoList(ListLayoutAssignmentVO.class, "ListListLayoutAssignment", paramMap);
		Map<String, Object> assignments = new HashMap<String, Object>();
		ListLayoutVO defaultLayoutVO = getDefaultListLayout(objId, clientType);
		Map<String, Object> defaultLayoutMap = new HashMap<String, Object>();
		defaultLayoutMap.put("id", defaultLayoutVO.getId());
		defaultLayoutMap.put("name", defaultLayoutVO.getName());
		for (ListLayoutAssignmentVO layoutAssignmentVO:assignmentVOs) {
			String keyString = layoutAssignmentVO.getRoleId() + "";
			// TODO 改成批量取数据，或者做缓存
			ListLayoutVO layoutVO = getPrimitiveListLayout(layoutAssignmentVO.getListLayoutId());
			Map<String, Object> layoutMap = new HashMap<String, Object>();
			layoutMap.put("id", layoutVO.getId());
			layoutMap.put("name", layoutVO.getName());
			assignments.put(keyString, layoutMap);
		}
		for (RoleVO roleVO: roleVOs) {
				String keyString = roleVO.getId()+"" ;
				if (!assignments.containsKey(keyString)) {
					assignments.put(keyString, defaultLayoutMap);
				}
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("roles", roleMaps);
		resultMap.put("assignments", assignments);
		return resultMap;
	}
	
	public ListLayoutVO getPrimitiveListLayout(Long id) {
		String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("tenantId", tenantId);
		paramMap.put("id", id);
		List<ListLayoutVO> listLayoutList =  dao.selectVoList(ListLayoutVO.class, "ListListLayout", paramMap);
		ListLayoutVO aListLayoutVO = new ListLayoutVO();
		for (ListLayoutVO listLayoutVO : listLayoutList) {
			aListLayoutVO = listLayoutVO;
			if (listLayoutVO.getTenantId() != 0) {
					break;
			}
		}
		return aListLayoutVO;
	}
	
	public AppListVO getAppListVOForCurrentUser(Long objId ){
		ListLayoutVO listLayoutVO = getListLayoutForCurrentUser(objId, ClientType.MOBILE.getIndex());
		AppListVO appListVO = new AppListVO();
		List<List<AppListValueVO>> appValueLists = new ArrayList<List<AppListValueVO>>();
		for (List<ListLayoutItemVO> listLayoutItemVOList: listLayoutVO.getItemsForClient()) {
			List<AppListValueVO> appListValueVOs = new ArrayList<AppListValueVO>();
			for (ListLayoutItemVO listLayoutItemVO:listLayoutItemVOList) {
				AppListValueVO appListValueVO = new AppListValueVO();
				appListValueVO.setItemkey(listLayoutItemVO.getApiName());
				appListValueVOs.add(appListValueVO);
			}
			appValueLists.add(appListValueVOs);
		}
		appListVO.setValuelist(appValueLists);
		return appListVO;
	}
	
	public void saveListLayoutDisableStatus(Long listLayoutId){
		ListLayoutVO listLayout = new ListLayoutVO();
		listLayout.setId(listLayoutId);
		listLayout.setEnableState(2);
		updateListLayoutOnly(listLayout);
	}
	
	public void saveListLayoutEnableStatus(Long listLayoutId){
		ListLayoutVO listLayout = new ListLayoutVO();
		listLayout.setId(listLayoutId);
		listLayout.setEnableState(1);
		updateListLayoutOnly(listLayout);
	}
	
	private void updateListLayoutOnly(ListLayoutVO listLayout) {
		String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		String userIdStr = (String) InvocationInfoProxy.getParameter("id");// 用户id
		Long userId = Long.parseLong(userIdStr);

		listLayout.setTenantId(tenantId);
		ListLayoutVO originalListLayout = getListLayout(listLayout.getId());
		disableCheck(listLayout);
		// 不是为了更新，而是为了返回用
		listLayout.setIsDefault(originalListLayout.getIsDefault());
		Date date = new Date();
		listLayout.setEnableTime(date);
		listLayout.setEnableUserId(userId);
		listLayout.setModifiedTime(date);
		listLayout.setSysModifiedTime(date);
		listLayout.setSysModifiedUserId(userId);
		listLayout.setModifiedUserId(userId);

		if (originalListLayout.getTenantId() == 0) {
			listLayout.setSysCreatedTime(date);
			listLayout.setCreatedTime(originalListLayout.getCreatedTime());
			listLayout.setObjId(originalListLayout.getObjId());
			listLayout.setSortOrder(originalListLayout.getSortOrder());
			super.saveVO(listLayout);
		} else {
			super.updateVO(listLayout);
		}
	}
	
	private void disableCheck(ListLayoutVO listLayout) {
		String userIdStr = (String)InvocationInfoProxy.getParameter("id");//用户id
		Long userId = Long.parseLong(userIdStr);
		if (!administratorManage.isAdmin(userId)) {
			throw new CrmBusinessException("没有该功能的操作权限");
		}
		if (listLayout.getIsDefault() == 1) {
			throw new CrmBusinessException("不能停用默认业务类型");
		}
	}
}
